package com.smh.dao;

import com.smh.model.User;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class UserDao {
    private List<User> users = new ArrayList<>();

    public boolean add(User user){
        try {
            this.users.add(user);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public boolean delete(String idcardNo){
        try{
            for(int i=0;i<this.users.size();i++){
                if(this.users.get(i).getIdcardNo().equals(idcardNo)){
                    this.users.remove(i);
                }
            }
            return true;
        }catch (Exception e){
            return false;
        }

    }

    public List<User> search(){
        return this.users;
    }

    public boolean update(User user) {
        String id = user.getIdcardNo();
        for (int i = 0; i < users.size(); i++) {
            User user1 = users.get(i);
            if (user1.equals(user)){
                user1.setName(user.getName());
                user1.setHead(user.getHead());
                user1.setAge(user.getAge());
                return true;
            }
        }
        return false;
    }
}
